package
{
	import com.naiking.core.NMovieClip;
	import com.naiking.core.SwfLoader;
	import com.naiking.data.enum.AlignType;
	import com.naiking.interfaces.IPoolItem;
	import com.naiking.manager.DragManager;
	import com.naiking.manager.ObjectPoolManager;
	import com.naiking.manager.TipManager;
	import com.naiking.ui.FButton;
	import com.naiking.util.NString;
	import com.naiking.util.UIUtil;
	import com.test.Dis;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.system.SecurityPrivilege;
	import flash.text.TextField;

	[SWF(frameRate="24", width="800", height="600")]
	public class UserMYLib extends Sprite
	{
		private var dynamicForTip:int;
		public function UserMYLib()
		{
			this.addEventListener(Event.ADDED_TO_STAGE,onInit);
			
		}
		
		protected function onInit(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,onInit);
			var sp:Sprite=new Sprite();
			UIUtil.addDisplayObject(sp,this,100,100);
			UIUtil.drawRectTo(sp,0xffccdd,1,0,0,200,100);
			
			testTip(sp);
			//string测试
			testStr();
			
			testTip(sp);
			//test objPool
			testPool();
			
			testMovie();
			
			
			
		}
		
		private function testMovie():void
		{
			
			var mc:NMovieClip=new NMovieClip();
			mc.mouseEnabled=false;
			UIUtil.addDisplayObject(mc,this,100,100);
			SwfLoader.getSwfBmpdtBySignal("../assets/c.swf",function(frms:Vector.<BitmapData>):void
			{
				mc.setFramedBmpdatas(frms);
				mc.play(0);
			});
				
		}
		
		private function testPool():void
		{
			com.test.Dis;//必须要有的 否则initPool的时候 getdefinitionByName找不到
			ObjectPoolManager.getInstance().initPool("com.test.Dis",10);
			var btn:FButton=new FButton();
			btn.lable="创建对象";
			UIUtil.addDisplayObject(btn,this,0,30);
			btn.addEventListener(MouseEvent.CLICK,onCreate);
		}
		private function testTip(sp:Sprite):void
		{
			//TipManager.addTip("dddd",sp,AlignType.ALIGN_BOTTOM);
			TipManager.addTip("",sp,"right",0,0,true,callTip);
			DragManager.addDrag(sp);
		}
		private function testStr():void
		{
			var txt:TextField=new TextField();
			txt.autoSize="left";
			UIUtil.addDisplayObject(txt,this,0,0);
			txt.text="abcd的的";
			trace(NString.getBytesLength(txt.text)+"个宽度单位")
		}
		/**从对象池获取*/
		protected function onCreate(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			var dis:Dis=ObjectPoolManager.getInstance().borrowItem("com.test.Dis") as Dis;
			dis.addEventListener(MouseEvent.CLICK,onReturnToPool);
			UIUtil.addDisplayObject(dis,this,Math.random()*800,30+Math.random()*400);
		}
		
		protected function onReturnToPool(event:MouseEvent):void
		{
			ObjectPoolManager.getInstance().returnItem("com.test.Dis",event.target as IPoolItem);
			this.removeChild(event.target as DisplayObject);	
		}
		private function callTip():String
		{
			dynamicForTip++;
			return dynamicForTip+" 个金币";
		}
	}
}